package com.aiyige.page.filter.model;


import com.aiyige.model.commontag.util.CommonTagUtil;
import com.aiyige.utils.ListUtil;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class FilterInfo {
    String filterCode;
    List<String> filterValue;

    public FilterInfo() {
    }

    private FilterInfo(Builder builder) {
        setFilterCode(builder.filterCode);
        setFilterValue(builder.filterValue);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getFilterCode() {
        return filterCode;
    }

    public void setFilterCode(String filterCode) {
        this.filterCode = filterCode;
    }

    public List<String> getFilterValue() {
        return filterValue;
    }

    public void setFilterValue(List<String> filterValue) {
        this.filterValue = filterValue;
    }

    public static final class Builder {
        private String filterCode;
        private List<String> filterValue;

        private Builder() {
        }

        public Builder filterCode(String val) {
            filterCode = val;
            return this;
        }

        public Builder filterValue(List<String> val) {
            filterValue = val;
            return this;
        }

        public FilterInfo build() {
            return new FilterInfo(this);
        }
    }

    public static List<FilterInfo> parse(List<Filter> filterList) {
        List<FilterInfo> filterInfoList = new LinkedList<>();
        if (ListUtil.isEmpty(filterList)) return filterInfoList;
        for (Filter filter : filterList) {
            switch (filter.getType()) {
                case Filter.TYPE_ADDRESS:
                    filterInfoList.add(FilterInfo.newBuilder()
                            .filterCode(filter.getId())
                            .filterValue(Arrays.asList(filter.getCityId()))
                            .build());
                    break;
                case Filter.TYPE_MULTI_SELECT_SMALL_RECT:
                case Filter.TYPE_SINGLE_SELECT_BIG_RECT:
                case Filter.TYPE_SINGLE_SELECT_SMALL_RECT:
                case Filter.TYPE_MULTI_SELECT_BIG_RECT:
                    filterInfoList.add(FilterInfo.newBuilder()
                            .filterCode(filter.getId())
                            .filterValue(CommonTagUtil.extractIdList(filter.getTagList()))
                            .build());
                    break;
                case Filter.TYPE_TIME_RANGE:
                    long startTime = filter.getStartTime() == null ? Long.MIN_VALUE : filter.getStartTime();
                    long endTime = filter.getEndTime() == null ? Long.MAX_VALUE : filter.getEndTime();
                    filterInfoList.add(FilterInfo.newBuilder()
                            .filterCode(filter.getId())
                            .filterValue(Arrays.asList(String.valueOf(startTime), String.valueOf(endTime)))
                            .build());
                    break;
            }
        }
        return filterInfoList;
    }
}
